package com.store.framework.gui.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.SwingUtilities;

import com.store.framework.gui.model.CalculatorModel;
import com.store.framework.gui.view.CalculatorView;

public class CalculatorController {
	private CalculatorModel m_model;
	private CalculatorView m_view;

	public CalculatorController(CalculatorModel model, CalculatorView view) {
		this.m_model = model;
		this.m_view = view;
		view.addShowLengthListener(new ShowLengthListener());
		view.addClearListener(new ClearListener());
	}
	public void show() {
		m_view.setVisible(true);
	}

	public void hide() {
		m_view.setVisible(false);
	}
	class ShowLengthListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent action) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					m_model.setValue(m_view.getUserInput().length()+"");
					m_view.setTotal(m_model.getValue());
				}
			});
		
		}
	}
	class ClearListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent action) {
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					m_view.reset();
				}
			});
		
			
		}
		
	}
}
